﻿using System;
using System.Text;
using System.IO;
using SeleniumToolkit.Config;

namespace SeleniumToolkit.Core
{

    /// <summary>
    /// Constructs arguments for the selenium-remote control command line
    /// </summary>
    public class SeleniumRemoteControlArgumentBuilder
    {
        #region Constructor
        /// <summary>
        /// Default Constructor
        /// </summary>
        /// <param name="config">Selenium Configuration object</param>
        public SeleniumRemoteControlArgumentBuilder(SeleniumConfiguration config)
        {
            _config = config;
        } 
        #endregion

        /// <summary>
        /// Gets the constructed arguments for the Remote Control Process
        /// </summary>
        /// <returns></returns>
        public string GetArguments()
        {
            var sb = new StringBuilder();

            AppendServerJar(sb);
            AppendProxyServer(sb);
            AppendUserExtensions(sb);

            return sb.ToString();
        }

        #region Private members
        private void AppendServerJar(StringBuilder arguments)
        {
            string filePath = Path.Combine(_config.RuntimeSettings.SeleniumRuntimePath, SeleniumRemoteControlJarName);
            if (!File.Exists(filePath))
                throw new FileNotFoundException(String.Format("Unable to locate the selenium remote-control library (selenium-server.jar), is the folder path \"{0}\" correct?", filePath), filePath);

            arguments.AppendFormat(" -jar \"{0}\"", filePath);
        }

        private void AppendProxyServer(StringBuilder arguments)
        {
            SeleniumConfigurationRuntimeElement runtimeSettings = _config.RuntimeSettings;
            if (HasProxyServerSetting())
            {
                arguments.AppendFormat(" -Dhttp.proxyHost={0} -Dhttp.proxyPort={1}", runtimeSettings.ProxyServer, runtimeSettings.ProxyPort);
            }
            if (HasAuthenticatedProxyServerSetting())
            {
                string password = runtimeSettings.ProxyPassword ?? String.Empty;
                arguments.AppendFormat(" -Dhttp.proxyUser={0} -Dhttp.proxyPassword={1}", runtimeSettings.ProxyUser, password);
            }
        }

        private void AppendUserExtensions(StringBuilder sb)
        {
            string userExtensionsFile = _config.UserExtensions.GetExtensionsFile();
            if (!String.IsNullOrEmpty(userExtensionsFile))
            {
                sb.AppendFormat(" -userExtensions \"{0}\"", userExtensionsFile);
            }
        }

        private bool HasProxyServerSetting()
        {
            return (!String.IsNullOrEmpty(_config.RuntimeSettings.ProxyServer));
        }

        private bool HasAuthenticatedProxyServerSetting()
        {
            return (!String.IsNullOrEmpty(_config.RuntimeSettings.ProxyUser));
        } 
        #endregion

        private const string SeleniumRemoteControlJarName = "selenium-server.jar";
        private readonly SeleniumConfiguration _config;
    }
}
